package com.benio.ycfdisplayer;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.benio.ycfdisplayer.displayer.DisplayListener;
import com.benio.ycfdisplayer.displayer.SimpleDisplayListener;
import com.benio.ycfdisplayer.displayer.YcfDisplayer;
import com.benio.ycfdisplayer.fresco.FrescoDisplayOptions;
import com.facebook.drawee.generic.RoundingParams;

public class PhotoViewActivity extends AppCompatActivity {
    private static final String TAG = "Fresco";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * Fresco与PhotoView结合的注意问题
         * 1.使用FrescoPhotoView，而不要直接使用PhotoView
         * 2.第一次从网络加载图片，渐变效果没有完全消失，照片会稍微模糊
         *   (可通过向下变换TopLevelDrawable，获取FadeDrawable，调用finishTransitionImmediately方法)
         * 3.需要使用retryImage的时候，需要在失败的时候需调用PhotoView的photoView.setZoomable(false)
         *   PhotoViewExecutor已在内部调用，无需手动调用。使用其他Executor则需要手动调用
         */
        YcfDisplayer.initialize(this);
        setContentView(R.layout.activity_photo_view);

        String uri1 = "http://g.hiphotos.baidu.com/baike/w%3D268%3Bg%3D0/sign=a4d8ef3bbd096b63811959563408e079/c2fdfc039245d6887aa19ab0a7c27d1ed21b2406.jpg";
        String uri2 = "http://i1.hdslb.com/bfs/archive/a3810e6dacd304ef8103980114a24b11e39659bf.jpg";

        final View view1 = findViewById(R.id.image1);
        final View view2 = findViewById(R.id.image2);
        final FrescoDisplayOptions options = new FrescoDisplayOptions.Builder()
                .setPlaceHolderImage(new ColorDrawable(Color.GREEN))
                .setFailureImage(new ColorDrawable(Color.RED))
                .setRoundingParams(new RoundingParams().setCornersRadii(100, 100, 100, 100))
                .setFadeDuration(900)
                .setTapToRetryEnabled(true)
                .setRetryImage(ContextCompat.getDrawable(this, R.mipmap.ic_launcher))
                .setDiskCacheEnabled(false)
                .build();
        DisplayListener listener = new SimpleDisplayListener() {
            @Override
            public void onStart(String imageUri, View view) {
                Log.d(TAG, "onStart() called with: imageUri = [" + imageUri + "], view = [" + view + "]");
            }

            @Override
            public void onSuccess(String imageUri, final View view, Object loadedData) {
                Log.d(TAG, "onSuccess() called with: imageUri = [" + imageUri + "], view = [" + view + "], loadedData = [" + loadedData + "]");
            }

            @Override
            public void onFailure(String imageUri, View view, Throwable throwable) {
                Log.d(TAG, "onFailure() called with: imageUri = [" + imageUri + "], view = [" + view + "], throwable = [" + throwable + "]");
            }
        };
        YcfDisplayer.get().display(uri1).options(options).listener(listener).into(view1);
        YcfDisplayer.get().display(uri2).options(options).listener(listener).into(view2);
    }
}
